package com.iflytek.exam.utils;

import java.util.HashSet;
import java.util.Random;
import java.util.Set;

/**
 * 随机组成试卷
 */
public class RandomCom {
//    @Autowired
//    private ICreateExamService createExamService;
//    @Autowired
//    private IChoiceService choiceService;
//    @Autowired
//    private QuestionExamService questionExamService;
//    @Autowired
//    private IJudgeService judgeService;
//    @Autowired
//    private IShortAnswerService shortAnswerService;
    //接收五个参数 1.单选数量，2.多选数量，3.判断数量，4.简答数量，5.试卷所属的uuid，6.学生ids
//    public boolean random(Long radio, Long check, Long judge, Long shortAnswer, String uuid, Long[] ids){
//        try {
//        //查询所有的单选的id（数组）
//        Long[] radioIds =choiceService.getAllChoiceRadioIds();
//        //查询所有多选的id（数组）
//        Long[] checkIds =choiceService.getAllChoiceCheckIds();
//        //查询所有的判断题id（数组）
//        Long[] judgeIds =judgeService.getAllJudgeIds();
//        //查询所有简答题的id（数组）
//        Long[] shortAnswerIds =shortAnswerService.getAllShortAnswerIds();
//
//        Long examId = createExamService.getExamIdByUuid(uuid); //试卷的id
//        System.out.println("examId:"+examId);
//
//        //1.获取所有被选中学生的ids
//        for (int i = 0; i < ids.length; i++){
//            Long user_id = ids[i]; //学生的user_id
//            //2.选择题随机组卷
//            if (radio > 0){
//                // 调用方法抽取 ID，这就是抽取到的所有选择题的ids
//                Long[] randomChoiceIds = getRandomChoiceIds(radioIds, radio);
//                for (int j = 0; j < randomChoiceIds.length; j++){
//                    QuestionExam questionExam = new QuestionExam();
//                    Long choiceId = randomChoiceIds[j]; //选择题的id
//                    //查找单选题的类型
//                    //3.将学生id，选择题id，试卷id插入到试卷试题表
//                    questionExam.setUserId(user_id);
//                    questionExam.setExamId(examId);
//                    questionExam.setQuestionId(choiceId);
//                    questionExam.setType(1);
//                    questionExamService.insertQuestionExam(questionExam);
//                }
//            }
//            if (check > 0){
//                // 调用方法抽取 ID，这就是抽取到的所有多选题的ids
//                Long[] randomChoiceCheckIds = getRandomChoiceIds(checkIds, check);
//                for (int j = 0; j < randomChoiceCheckIds.length; j++){
//                    QuestionExam questionExam = new QuestionExam();
//                    Long choiceCheckId = randomChoiceCheckIds[j]; //多选题的id
//                    //查找选择题的类型
//                    //3.将学生id，选择题id，试卷id插入到试卷试题表
//                    questionExam.setUserId(user_id);
//                    questionExam.setExamId(examId);
//                    questionExam.setQuestionId(choiceCheckId);
//                    questionExam.setType(2);
//                    questionExamService.insertQuestionExam(questionExam);
//                }
//            }
//            if (judge > 0){
//                // 调用方法抽取 ID，这就是抽取到的所有多选题的ids
//                Long[] randomChoiceJudgeIds = getRandomChoiceIds(judgeIds, judge);
//                for (int j = 0; j < randomChoiceJudgeIds.length; j++){
//                    QuestionExam questionExam = new QuestionExam();
//                    Long JudgeId = randomChoiceJudgeIds[j]; //多选题的id
//                    //查找选择题的类型
//                    //3.将学生id，选择题id，试卷id插入到试卷试题表
//                    questionExam.setUserId(user_id);
//                    questionExam.setExamId(examId);
//                    questionExam.setQuestionId(JudgeId);
//                    questionExam.setType(3);
//                    questionExamService.insertQuestionExam(questionExam);
//                }
//            }
//            if (shortAnswer > 0){
//                // 调用方法抽取 ID，这就是抽取到的所有多选题的ids
//                Long[] randomShortAnswerIdsIds = getRandomChoiceIds(shortAnswerIds, shortAnswer);
//                for (int j = 0; j < randomShortAnswerIdsIds.length; j++){
//                    QuestionExam questionExam = new QuestionExam();
//                    Long shortAnswerId = randomShortAnswerIdsIds[j]; //多选题的id
//                    //查找选择题的类型
//                    //3.将学生id，选择题id，试卷id插入到试卷试题表
//                    questionExam.setUserId(user_id);
//                    questionExam.setExamId(examId);
//                    questionExam.setQuestionId(shortAnswerId);
//                    questionExam.setType(4);
//                    questionExamService.insertQuestionExam(questionExam);
//                }
//            }
//        }
//            return true;
//        } catch (Exception e) {
//            // 如果发生异常，打印堆栈跟踪（可选）
//            e.printStackTrace();
//            // 返回 false 表示发生错误
//            return false;
//        }
//
//    }
    public static Long[] getRandomChoiceIds(Long[] whatIds, Long which) {
        if (whatIds == null || whatIds.length < which) {
            throw new IllegalArgumentException("数组长度不足或参数非法");
        }

        Set<Long> selectedSet = new HashSet<>();
        Random random = new Random();
        while (selectedSet.size() < which) {
            int index = random.nextInt(whatIds.length);
            Long id = whatIds[index];
            selectedSet.add(id); // 确保不重复添加
        }

        // 将 Set 转换为数组
        Long[] result = new Long[selectedSet.size()];
        return selectedSet.toArray(result);
    }
}
